package dubboRpc.consumer;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @author: zlz
 * @date: 2021/9/15 22:29
 * @description:
 * @since: JDK1.8
 */
public class DubboClientHandler extends ChannelInboundHandlerAdapter implements Callable {
    ChannelHandlerContext context;
    String res;
    String param;
    @Override
    public synchronized Object call() throws Exception {
        context.writeAndFlush(param);
        wait();
        return res;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        res = msg.toString();
        notify();
    }

    public void setParam(String param) {
        this.param = param;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.context = ctx;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
